<html>
<head><meta charset="utf-8"><title>ExactSizeIterator vs TrustedLen · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/ExactSizeIterator.20vs.20TrustedLen.html">ExactSizeIterator vs TrustedLen</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="235531766"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/ExactSizeIterator%20vs%20TrustedLen/near/235531766" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Mazakas <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/ExactSizeIterator.20vs.20TrustedLen.html#235531766">(Apr 21 2021 at 15:30)</a>:</h4>
<p>Hey everyone, </p>
<p>I'm having some trouble understanding the difference between <code>ExactSizeIterator</code> and <code>TrustedLen</code> and why  being <code>unsafe</code> matters.</p>
<p>I'm also wondering how long until the trait gets stabilized as the stdlib specializes quite a bit on <code>TrustedLen</code> yet normal users of the stable toolchain can't enjoy those performance benefits.</p>



<a name="235534039"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/ExactSizeIterator%20vs%20TrustedLen/near/235534039" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/ExactSizeIterator.20vs.20TrustedLen.html#235534039">(Apr 21 2021 at 15:44)</a>:</h4>
<p>The difference between <code>ExactSizeIterator</code> and <code>TrustedLen</code> is that in the former case lying about the size is merely a logic bug and may cause a panic, in the latter case it is a safety issue and may cause UB. For this reason <code>TrustedLen</code> is marked as <code>unsafe</code>.</p>



<a name="235540385"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/ExactSizeIterator%20vs%20TrustedLen/near/235540385" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/ExactSizeIterator.20vs.20TrustedLen.html#235540385">(Apr 21 2021 at 16:25)</a>:</h4>
<p>Note also that they have materially different rules.</p>
<p><code>Skip</code> and <code>Chain</code> help emphasize the differences.  ESI needs to be able to represent the length as a <code>usize</code>, so it can still know the remaining length after a <code>Skip</code>, but that might not be true after a <code>Chain</code> because the addition could overflow.  Whereas overflow is ok for TrustedLen, so it can be maintained through a <code>Chain</code>, but not through a <code>Skip</code> because once it's overflowed, the subtraction is no longer going to give the right answer.</p>
<p>Basically, ESI is just to make <code>rposition</code> work, and because it's handy to have a <code>.len()</code> sometimes.</p>



<a name="235550731"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/ExactSizeIterator%20vs%20TrustedLen/near/235550731" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> The 8472 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/ExactSizeIterator.20vs.20TrustedLen.html#235550731">(Apr 21 2021 at 17:33)</a>:</h4>
<p>While TrustedLen is used in various optimizations around consuming iterators, e.g. allocating once and then just slotting iterator results into the allocated collection without any additional bounds or capacity checks.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>